﻿
var controller = this.lookupController();
var status = controller.analysis().status;
if (status == 'launch' || status == 'resubmit') {
    var record = store.getUpdatedRecords()[0],
        flag = 'update';

    if (!record) {
        record = store.getNewRecords()[0];
        flag = 'add';
    }

    if (!record) {
        flag = 'reset';
    }

    if (record) {
        var grid3 = ref.datagrid3,
            store3 = grid3.getStore();
        if (flag == 'update') {
            if (record.modified) {

                if (record.modified.DescriptionOfSuppliesServices !== undefined) {
                    var r = store3.findRecord('DescriptionOfSuppliesServices', record.data.DescriptionOfSuppliesServices);
                    record.data.ProjectActivity = r.data.ProjectActivity;
                } else if (record.modified.JASharingRatio !== undefined) {
                    if (!Ext.isEmpty(record.data.JASharingRatio)) {
                        if (parseFloat(record.data.JASharingRatio) < 0 || parseFloat(record.data.JASharingRatio) > 100) {
                            record.set("JASharingRatio", 0);
                        } else {
                            record.data.LRSharingRatio = 100 - record.data.JASharingRatio;
                            for (var i = 0; i < store3.getCount(); i++) {
                                var DescriptionOfSuppliesServices = store3.getAt(i).data.DescriptionOfSuppliesServices;
                                var cost = store3.getAt(i).data.Cost;
                                var company = store3.getAt(i).data.Company;
                                if (DescriptionOfSuppliesServices == record.data.DescriptionOfSuppliesServices && company == 'CJLR') {
                                    record.data.JACost = parseFloat(((cost * record.data.JASharingRatio) / 100).toFixed(2));
                                    record.data.LRCost = parseFloat(((cost * record.data.LRSharingRatio) / 100).toFixed(2));
                                }
                            }
                        }

                    } else if (record.modified.LRBrandSharingRatio !== undefined) {
                        if (!Ext.isEmpty(record.data.LRSharingRatio)) {
                            if (parseFloat(record.data.LRSharingRatio) < 0 || parseFloat(record.data.LRSharingRatio) > 100) {
                                record.set("LRSharingRatio", 0);
                            } else {
                                record.data.JASharingRatio = 100 - record.data.LRSharingRatio;
                                for (var i = 0; i < store3.getCount(); i++) {
                                    var DescriptionOfSuppliesServices = store3.getAt(i).data.DescriptionOfSuppliesServices;
                                    var cost = store3.getAt(i).data.Cost;
                                    var company = store3.getAt(i).data.Company;
                                    if (DescriptionOfSuppliesServices == record.data.DescriptionOfSuppliesServices && company == 'CJLR') {
                                        record.data.JACost = parseFloat(((cost * record.data.JASharingRatio) / 100).toFixed(2));
                                        record.data.LRCost = parseFloat(((cost * record.data.LRSharingRatio) / 100).toFixed(2));
                                    }
                                }
                            }
                        }
                    }
                }

            }
            record.commit();
        } else if (flag == 'add') {
            record.commit();
        }
    }
}
